/*
 * This packet is a request to log into the system
 *
 * @author Jonathan Walsh (jwalsh8484@gmail.com)
 */

package egs.packets;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;

public class AuthLoginPacket extends Packet {

    String user_name;
    String password;
    short[][] game_list;
    short game_list_len;

    public AuthLoginPacket( String user_name, String password, 
                            short[][] game_list ) throws PacketException {
        
        /*Set up the header*/
        message_type = MSG_AUTH_LOGIN;
        game_name = 0;
        game_version = 0;
        
        /*fill in rest of packet*/
        if ( user_name.length() > 30 ) {
            throw new PacketException( "Invalid user length (must be <30)" );
        } else if ( password.length() > 30 ) {
            throw new PacketException("Invalid password length (must be <30)");
        }
        this.user_name = user_name;
        this.password = password;
        
        /*game list*/
        this.game_list = game_list;
        game_list_len = (short)game_list.length;
        
        data_area_size = 4 + 30*2 + 30*2 + game_list_len*4;
    }
    
    public AuthLoginPacket( byte[] raw ) {
        ByteBuffer buf = setHeaderNew( raw );
        
        game_list_len = buf.getShort();
        short empty = buf.getShort();
        
        /*get user name*/
        char[] un = new char[30];
        for ( int i = 0; i < 30; i++ )
            un[i] = buf.getChar();
        user_name = new String(un);
        user_name = user_name.substring(0, user_name.indexOf('\0'));
        
        /*get password*/
        char[] pw = new char[30];
        for ( int i = 0; i < 30; i++ )
            pw[i] = buf.getChar();
        password = new String(pw);
        password = password.substring(0, password.indexOf('\0'));
        
        /*put in game list*/
        game_list = new short[game_list_len][2];
        for ( int i = 0; i < game_list_len; i++ ) {
            game_list[i][0] = buf.getShort();
            game_list[i][1] = buf.getShort();
        }
    }
	
    public byte[] getRaw() {
        ByteBuffer buf = makeBufferWithHeader( data_area_size );
        buf.putShort( game_list_len );
        buf.putShort( (short)0 );
        
        /*put in the username*/
        int i = 0;
        for ( ; i < user_name.length(); i++ )
            buf.putChar( user_name.charAt( i ) );
        for ( ; i < 30; i++ ) 
            buf.putChar( '\0' );
        
        /*put in the password*/
        i = 0;
        for ( ; i < password.length(); i++ )
            buf.putChar( password.charAt( i ) );
        for ( ; i < 30; i++ ) 
            buf.putChar( '\0' );
        
        /*put in game list*/
        for ( i = 0; i < game_list_len; i++ ) {
            buf.putShort( game_list[i][0] );
            buf.putShort( game_list[i][1] );
        }
        
        return buf.array();
    }
    
    
    public String toString() {
        String ret = super.toString() + "\ngame_list_len = " + game_list_len;
        ret = ret + "\nuser_name=" + user_name;
        ret = ret + "\npassword=" + password;
        ret = ret + "\ngame_list=";
        for ( int i = 0; i < game_list_len; i++ ) {
            ret = ret + "\n  game("+i+")="+game_list[i][0] + "," + game_list[i][1];
        }
        return ret;
    }
    
    public String get_user_name() { return user_name; }
    public String get_password() { return password; }
    public short[][] get_game_list() { return game_list; }
    
}
